<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.typeName"
          clearable
          placeholder="输入姓名"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->

      <crudOperation :permission="permission"/>
      <!--表单组件-->
      <el-dialog
        @open="handleOpen"
        @close="doSubmit"
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="标题" prop="typeName">
            <el-input v-model="form.typeName" style="width: 370px;" maxlength="11" placeholder="标题"/>
          </el-form-item>
          <el-form-item>
            <el-upload
              ref="uploadImg"
              action="/api/upload/toImg"
              :headers="headers"
              list-type="picture-card"
              :file-list="fileList"
              limit="1"
              :on-success="handleSuccess"
              :on-preview="handlePictureCardPreview"
              :before-remove="handleBeforeRemove"
              :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="form.dialogImageUrl" alt="">
            </el-dialog>
          </el-form-item>


          <el-form-item label="长度" prop="length">
            <el-input v-model="form.length" style="width: 100%;" maxlength="5" placeholder="长度"/>
          </el-form-item>


          <el-form-item label="宽度" prop="width">
            <el-input v-model="form.width" style="width: 100%;" maxlength="5" placeholder="宽度"/>
          </el-form-item>


          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" style="width: 370px;" maxlength="5" placeholder="排序"/>
          </el-form-item>

          <el-form-item label="描述" prop="describe">
            <el-input v-model="form.describe" style="width: 370px;" type="textarea" maxlength="100" placeholder="描述"
                      rows="3"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU" @close="doSubmit">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU" @close="doSubmit">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->


      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column :selectable="checkboxT" type="selection" width="55"/>
        <el-table-column :show-overflow-tooltip="true" prop="typeName" label="标题"/>

        <el-table-column
          label="图片"
          width="180">
          <template scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.url"
              fit="center"></el-image>
          </template>

        </el-table-column>

        <el-table-column prop="enable" label="开启">
          <template slot-scope="scope">

            <el-switch
              @change="handelChange($event,scope.row.id)"
              v-model="scope.row.enable"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>


        <el-table-column :show-overflow-tooltip="true" prop="sort" label="排序"/>
        <el-table-column :show-overflow-tooltip="true" prop="describe" label="描述"/>
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>


        <el-table-column v-permission="['admin','testWater:edit','testWater:del']" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission"/>
          </template>
        </el-table-column>

      </el-table>
      <!--分页组件-->

      <pagination/>

    </div>
  </div>
</template>


<script>
  import crudTest from '@/api/wdtypes'   //数据 接口
  import {getToken} from '@/utils/auth'

  import CRUD, {presenter, header, form, crud} from '@crud/crud'  //


  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation' //列表 删除修改按钮
  import udOperation from '@crud/UD.operation'  //列表删除提示框


  // 分页 组件
  import pagination from '@crud/Pagination'


  // crud交由presenter持有
  //定义请求数据，
  const defaultCrud = CRUD({
    title: '',
    url: 'api/wdtypes',
    method: 'post',
    sort: 'id,desc',
    crudMethod: {...crudTest}
  })


  const defaultForm = {
    id:
      null,
    typeName:
      null,
    sort:
      null,
    is_delete:
      null,
    type_id:
      null,
    describe:
      null,
    createTime:
      null,
    length:
      null,
    width:
      null,
    type:
      null,

  }


  export default {
    name: 'Test',
    components: {pagination, crudOperation, rrOperation, udOperation},

    mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()], //请求列表数据


    data() {
      return {
        fileList:[],
        // 图片
        headers: {
          'Authorization': getToken()
        },
        dialogImageUrl:"",
        dialogVisible: false,
        permission: {
          add: ['admin', 'wdtypes:add'],
          edit: ['admin', 'wdtypes:edit'],
          del: ['admin', 'wdtypes:del']
        },
        rules: {

          typeName:
            [
              {required: true, message: '类型不能为空', trigger: 'blur'}
            ],
          sort:
            [
              {required: true, message: '排序不能为空', trigger: 'blur'}
            ],
          describe:
            [
              {required: true, message: '描述不能为空', trigger: 'blur'}
            ],
          length:
            [
              {required: true, message: '长度不能为空', trigger: 'blur'}
            ],
          width:
            [
              {required: true, message: '宽度不能为空', trigger: 'blur'}
            ],
        },


      }
    },


    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.form.dialogImageUrl = file.url;
        this.dialogVisible = true;

      },
      handleSuccess(file) {
        this.form.url = file;
        this.form.type = 'wdtypes';
      },
      handleBeforeRemove(file, fileList) {

      /*  for (let i = 0; i < this.fileList.length; i++) {
          if (this.fileList[i].uid === file.uid) {
            fileList.del(this.fileList[i].id).then(res => {})
            return true
          }
        }*/
      },
      // 刷新列表数据
      doSubmit() {

        this.dialogVisible = false
        this.form.dialogImageUrl = ''
        this.$refs.uploadImg.clearFiles()
      },
      handleOpen(){
        this.fileList=[];

        console.log( this.crud.status.add)
        if(typeof this.form.url != 'undefined' && this.crud.status.add==0){
          this.fileList.push({
            url:this.form.url

          })
        }

      },
      handelChange(el,id){

        this.form.enable=el;
        this.form.id=id;
        crudTest.edit(this.form);
      },

      // 获取数据前设置好接口地址
      [CRUD.HOOK.beforeRefresh]() {
        const query = this.query
        if (query.type && query.value) {
          this.crud.params[query.type] = query.value
        }
        return true
      }
    }
  }
</script>
<style scoped>
</style>
